#include "UltimateWinInputPluginPrecompiled.h"
#include "UltimateWinInputSubsystem.h"



UltimateWinInputSubsystem::UltimateWinInputSubsystem(Ogre::RenderWindow * window)
{
	Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
	OIS::ParamList pl;
	size_t windowHnd = 0;
	std::ostringstream windowHndStr;

	window->getCustomAttribute("WINDOW", &windowHnd);
	windowHndStr << windowHnd;
	pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

	mInputManager = OIS::InputManager::createInputSystem( pl );

	mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
	mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));

	mMouse->setEventCallback(this);
	mKeyboard->setEventCallback(this);
}
 
UltimateWinInputSubsystem::~UltimateWinInputSubsystem()
{

}


UltimateInputSubsystem::UltimateInputStruct * UltimateWinInputSubsystem::handleInput()
{
	/*for(std::map<std::string,int>::iterator i = inputStruct.inputMap.begin(); i!= inputStruct.inputMap.end(); i++)
	{
		(*i).second =0;
	}*/

	

	mKeyboard->capture();
	mMouse->capture();

	
	return &inputStruct;
}

void UltimateWinInputSubsystem::clearInput() {
	for (std::map<std::string, int>::iterator i = inputStruct.inputMap.begin();
			i != inputStruct.inputMap.end(); i++) {
		(*i).second = 0;
	}

}


bool UltimateWinInputSubsystem::keyPressed( const OIS::KeyEvent &arg )
{
	inputStruct.pressedButtons.insert(arg.key);

	return true;
}

bool UltimateWinInputSubsystem::keyReleased( const OIS::KeyEvent &arg )
{
	inputStruct.pressedButtons.erase(arg.key);

	return true;
}

bool UltimateWinInputSubsystem::mouseMoved( const OIS::MouseEvent &arg )
{

	inputStruct.inputMap[INPUT_POINTER_REL_X] =arg.state.X.rel/5;
	inputStruct.inputMap[INPUT_POINTER_REL_Y] =arg.state.Y.rel/-5;

	return true;
}

bool UltimateWinInputSubsystem::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{

	return true;
}

bool UltimateWinInputSubsystem::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{

	return true;
}

